/**
 *  jQuery.SelectListActions
 *  https://github.com/esausilva/jquery.selectlistactions.js
 *
 *  (c) http://esausilva.com
 */

(function ($) {
    //Moves selected item(s) from sourceList to destinationList
    $.fn.moveToList = function (sourceList, destinationList) {
        var opts = $(sourceList + ' option:selected');
        if (opts.length == 0) {
            alert("Nothing to move");
        }

        $(destinationList).append($(opts).clone());
    };

    //Moves all items from sourceList to destinationList
    $.fn.moveAllToList = function (sourceList, destinationList) {
        var opts = $(sourceList + ' option');
        if (opts.length == 0) {
            alert("Nothing to move");
        }

        $(destinationList).append($(opts).clone());
    };

    //Moves selected item(s) from sourceList to destinationList and deleting the
    // selected item(s) from the source list
    $.fn.moveToListAndDelete = function (sourceList, destinationList) {
        var opts = $(sourceList + ' option:selected');
        if (opts.length == 0) {
            alert("Nothing to move");
        }

        $(opts).remove();
        $(destinationList).append($(opts).clone());
    };

    //Moves all items from sourceList to destinationList and deleting
    // all items from the source list
    $.fn.moveAllToListAndDelete = function (sourceList, destinationList) {
        var opts = $(sourceList + ' option');
        if (opts.length == 0) {
            alert("Nothing to move");
        }

        $(opts).remove();
        $(destinationList).append($(opts).clone());
    };

    //Removes selected item(s) from list
    $.fn.removeSelected = function (list) {
        var opts = $(list + ' option:selected');
        if (opts.length == 0) {
            alert("Nothing to remove");
        }

        $(opts).remove();
    };

    //Moves selected item(s) up or down in a list
    $.fn.moveUpDown = function (list, btnUp, btnDown) {
        var opts = $(list + ' option:selected');
        if (opts.length == 0) {
            alert("Nothing to move");
        }

        if (btnUp) {
            opts.first().prev().before(opts);
        } else if (btnDown) {
            opts.last().next().after(opts);
        }
    };
    
    //filter the list box
    $.fn.filterByText = function(textbox, selectSingleMatch) {
    	  return this.each(function() {
    	    var select = this;
    	    var options = [];
    	    $(select).find('option').each(function() {
    	      options.push({value: $(this).val(), text: $(this).text()});
    	    });
    	    $(select).data('options', options);
    	    $(textbox).bind('change keyup', function() {
    	      var options = $(select).empty().scrollTop(0).data('options');
    	      var search = $.trim($(this).val());
    	      var regex = new RegExp(search,'gi');
    	 
    	      $.each(options, function(i) {
    	        var option = options[i];
    	        if(option.text.match(regex) !== null) {
    	          $(select).append(
    	             $('<option>').text(option.text).val(option.value)
    	          );
    	        }
    	      });
    	      if (selectSingleMatch === true && 
    	          $(select).children().length === 1) {
    	        $(select).children().get(0).selected = true;
    	      }
    	    });
    	  });
    	};
})(jQuery);
